/**
 * 
 */
package com.googlecode.rotools.rodb;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 
 */
class DefineTable {
	private final static String ISEXIST = "select tablename from sys.systables where tablename=?"; 
	
	private String tableName;
	private String createTable;
	
	DefineTable(String tableName, String createTable) {
		super();
		this.tableName = tableName;
		this.createTable = createTable;
	}

	String getTableName() {
		return tableName;
	}

	String getCreateTable() {
		return createTable;
	}
	boolean isExist(Connection connection) throws SQLException{
		PreparedStatement statement = connection.prepareStatement(ISEXIST);
		try{
			statement.setString(1, getTableName());
			
			ResultSet resultSet = statement.executeQuery();
			try{
				return resultSet.next();
				
			}finally{
				resultSet.close();
			}
		}finally{
			statement.close();
			
		}
	}
	void createTable(Connection connection) throws SQLException{
		Statement statement = connection.createStatement();
		try{
			statement.execute(getCreateTable());
			
		}finally{
			statement.close();
			
		}
	}
}
